package org.pomirkovany;

import java.io.IOException;
import java.util.Calendar;
import java.util.Hashtable;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.pomirkovany.beans.Item;
import org.pomirkovany.model.DataAccess;





public class CataloguerServlet extends javax.servlet.http.HttpServlet {
	
	
	private static final long serialVersionUID = 1L;
	DataAccess dataAccess = new DataAccess();

	  public CataloguerServlet() {
	    super();
	    }
	  
	public void init(ServletConfig config) throws ServletException{
		System.out.println("*** initializing controller servlet.");
		super.init(config);
		
		dataAccess.setDbURL(config.getInitParameter("dbURL"));
		dataAccess.setDbUserName(config.getInitParameter("dbUserName"));
		dataAccess.setDbPassword(config.getInitParameter("dbPassword"));

	    ServletContext context = config.getServletContext();
	    context.setAttribute("base", config.getInitParameter("base"));
	    context.setAttribute("imageURL", config.getInitParameter("imageURL"));
	    context.setAttribute("dataAccess", dataAccess);

	    try {  // load the database JDBC driver
	      Class.forName(config.getInitParameter("jdbcDriver"));
	      }
	    catch (ClassNotFoundException e) {
	      System.out.println(e.toString());
	      }
	}
	
	public void doGet (HttpServletRequest req, HttpServletResponse res)
		      throws ServletException, IOException {
		    doPost(req, res);
		    
		    }
	protected void doPost(HttpServletRequest request,
		      HttpServletResponse response) throws ServletException, IOException {
		    String base = "/jsp/";
		    String url = base + "index.jsp";
		    String action = request.getParameter("action");
		    String itemId = request.getParameter("itemId");
		    System.out.println("posting");
		    if (action != null) {
		    	if (action.equals("categorySelect"))
		            url = base + "category.jsp";
		    	else if(action.equals("itemDetails"))
		    		url = base + "item.jsp";
		    	else if(action.equals("deleteItem")){
		    		url = base + "category.jsp";
		    		System.out.println("item:" + itemId);
		    		dataAccess.deleteItemByID(itemId);		    		
		    	}
		    	else if(action.equals("addItem")){
		    		url = base + "additem.jsp";
		    		System.out.println("Item add");
		    	}
		    	else if(action.equals("editItem")){
		    		url = base + "edit.jsp";
		    		System.out.println("Item edit");
		    	}
		    	else if(action.equals("addCategory")){
		    		url = base + "addcategory.jsp";
		    		System.out.println("Item edit");
		    	}
		    	else if(action.equals("categoryEntered")){
		    		url = base + "category.jsp";
		    		String categoryName = request.getParameter("categoryname");
		    		dataAccess.addCategory(categoryName);
		    		System.out.println("category adding");
		    	}
		    	else if(action.equals("edited")){
		    		String id = request.getParameter("itemId");
		    		String itemName = request.getParameter("name");
		    		String itemAuthor = request.getParameter("author");
		    		String itemOwner = request.getParameter("owner");
		    		String itemLocation = request.getParameter("location");
		    		String itemComment = request.getParameter("comment");
		    		int rate = Integer.parseInt(request.getParameter("rate"));
		    		String borr = request.getParameter("borrowed");
		    		int category = dataAccess.getCategoryId(request.getParameter("category"));
		    		boolean borrowed = true;
		    		
		    		if (borr == null)
		    			 borrowed = false;	    		
	    		
		    			    		
		    		Item toEdit = new Item(itemName, itemAuthor, itemOwner, category,  borrowed, "none.png", itemLocation, rate, itemComment);
		    		dataAccess.editItem(toEdit, id);
		    		System.out.println("item" + id +  "edited");
		    	}
		    	else if(action.equals("search")){
		    		url = base + "searchresults.jsp";
		    		System.out.println(request.getParameter("keyword"));
		    	}
		    	else if(action.equals("itemEntered")){
		    				    		
		    		String Id = request.getParameter("itemId");
		    		String itemName = request.getParameter("name");
		    		String itemAuthor = request.getParameter("author");
		    		String itemOwner = request.getParameter("owner");
		    		String itemLocation = request.getParameter("location");
		    		String itemComment = request.getParameter("comment");
		    		int rate = Integer.parseInt(request.getParameter("rate"));
		    		String borr = request.getParameter("borrowed");
		    		int category = dataAccess.getCategoryId(request.getParameter("category"));
		    		boolean borrowed = true;
		    		
		    		if (borr == null)
		    			 borrowed = false;	    		
	    		
		    			    		
		    		Item toAdd = new Item(itemName, itemAuthor, itemOwner, category,  borrowed, "none.png", itemLocation, rate, itemComment);
		    		dataAccess.addItem(toAdd);
		    		
		    	}
		    	
		    		 
		        } else {
		    	  System.out.println("no action"); 
		      }
		    RequestDispatcher requestDispatcher =
		      getServletContext().getRequestDispatcher(url);
		    requestDispatcher.forward(request, response);
		    }
	
	
}
